#include <iostream>
#include <string.h>
using namespace std;

class cat
{
private:
    char *cat_name;
    int cat_weight;

public:
    void Set(char *name, int weight)
    {
        int len = strlen(name);
        cat_name = new char[len + 1];
        strcpy(cat_name, name);
        cat_weight = weight;
    }
    void print(cat p)
    {
        cout << p.cat_name << " ";
    }
    char *get_name()
    {
        return cat_name;
    }
    int get_weight()
    {
        return cat_weight;
    }
};

int main()
{
    int n, i, weight, j;
    char name[100];
    cat *p;
    cin >> n;
    p = new cat[n];
    for (i = 0; i < n; i++)
    {
        cin >> name >> weight;
        p[i].Set(name, weight);
    }
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            cat temp;
            if (p[i].get_weight() > p[j].get_weight())
            {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
    {
        p->print(p[i]);
    }
    delete p;
}
